package com.sntra.beibi.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.sntra.beibi.domain.Dot;
import com.sntra.beibi.domain.Pen;
import com.sntra.beibi.request.DotRequest;
import com.sntra.beibi.request.PenRequest;
import com.sntra.beibi.service.DotService;


@RestController
@Controller
public class DrawController {

	@Autowired
	private DotService dotService;
	
	@Autowired
	private StringRedisTemplate stringRedisTemplate;
	
	// 返回
		@RequestMapping(value = "/dot/demo", method = RequestMethod.GET)
		public String demo() {
			dotService.draw(1,101,5,"D8:0B:CB:61:46:76");
			return "OK";
		}
	// 返回
	@RequestMapping(value = "/dot/save", method = RequestMethod.POST)
	public String save(@RequestBody List<DotRequest>  drs, HttpServletRequest req) {
		int ownerID = 0,bookID = 0,pageNO = 0;
		try {
			List<Dot> dots = new ArrayList<Dot>();	
			String penMac = "";
			for(DotRequest dr : drs) {
				Dot dot = new Dot();
				BeanUtils.copyProperties(dr, dot);
				//ownerID = dr.getOwnerID();
				bookID = dr.getBookID();
				pageNO = dr.getPageID();
				penMac = dr.getDeviceMac();
				//dot.setSectionId(sectionID);
				dot.setBookId(bookID);
				//dot.setOwnerId(ownerID);
				dot.setPageId(pageNO);
				dot.setTimelong(dr.getTimeLong());
				dot.setType(dr.getDotType());
				dots.add(dot);
			}
			dotService.insertBatch(dots);
			String isDrawPdf = stringRedisTemplate.opsForValue().get("isDrawPdf_"+penMac);
			if((!StringUtils.hasText(isDrawPdf)) || "0".equals(isDrawPdf)) {
				stringRedisTemplate.opsForValue().set("isDrawPdf_"+penMac,"1", 60 ,TimeUnit.SECONDS);
				Thread.sleep(1000);
				//int num = dotService.count(sectionID, ownerID, bookID, pageNO);
				//if(num>=1000) {
				dotService.draw(ownerID,bookID,pageNO,penMac);
				//}
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "OK";
	}
	
	/**
	 * 保存笔的信息
	 * @param pen
	 * @param req
	 * @return
	 */
	@RequestMapping(value = "/pen/save", method = RequestMethod.POST)
	public String savePen(@RequestBody PenRequest  pr, HttpServletRequest req) {
		try {
			Pen p = dotService.getPen(pr.getDeviceMac());
			if(p!=null) {
				p.setUserId(pr.getUserId());
				p.setMacAddress(pr.getDeviceMac());
				p.setPenName(pr.getDeviceName());
				dotService.updatePen(p);
			}else {
				p = new Pen();
				p.setUserId(pr.getUserId());
				p.setMacAddress(pr.getDeviceMac());
				p.setPenName(pr.getDeviceName());
				dotService.savePen(p);
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "OK";
	}

	
}
